package com.jiangnan.view;

import com.jiangnan.constants.Constants;
import com.jiangnan.filter.PcapFileFilter;
import com.jiangnan.model.TableData;
import com.jiangnan.service.PacketService;
import com.jiangnan.utils.JTableUtil;
import com.jiangnan.utils.PacketUtil;
import com.jiangnan.utils.SwingUtil;
import jpcap.JpcapCaptor;
import jpcap.JpcapWriter;
import jpcap.packet.Packet;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;

/**
 *  工具栏配置
 *
 * @author chenliang
 * @email wschenliang@aliyun.com
 */
public class HomeJMenuBar extends JMenuBar {

    private final PacketService packetService = PacketService.newInstance();
    private final JFrame jFrame;
    private final JMenuItem openItem, saveItem, sendPacketItem, aboutItem, tipItem, cutItem, snifferItem;

    public HomeJMenuBar(JFrame jFrame) {
        this.jFrame = jFrame;
        this.setFont(new Font("微软雅黑", Font.PLAIN, 10));
        this.setPreferredSize(new Dimension(0,30));
        //------------------------------------------------------------
        openItem = SwingUtil.getMenuItem("打开", new OpenAction());
        saveItem = SwingUtil.getMenuItem("保存", new SaveAction());
        sendPacketItem = SwingUtil.getMenuItem("发送数据包", new SendPacketAction());
        aboutItem = SwingUtil.getMenuItem("关于", new AboutAction());
        tipItem = SwingUtil.getMenuItem("提示", new TipItemAction());
        cutItem = SwingUtil.getMenuItem("流量裁剪", new cutAction());
        snifferItem = SwingUtil.getMenuItem("流量捕获", new snifferAction());

        //-----------------------------------------------------------
        this.add(getFileMenu());
        this.add(getEditMenu());
        this.add(getToolMenu());
        this.add(getHelpMenu());
    }


    @Override
    public JMenu getHelpMenu() {
        JMenu menu = new JMenu("帮助");
        menu.add(tipItem);
        menu.add(aboutItem);
        return menu;
    }

    private Component getToolMenu() {
        JMenu menu = new JMenu("工具");
        menu.add(sendPacketItem);
        menu.add(cutItem);
        menu.add(snifferItem);
        return menu;

    }

    private Component getEditMenu() {
        JMenu menu = new JMenu("编辑");
        return menu;

    }

    private Component getFileMenu() {
        JMenu menu = new JMenu("文件");
        menu.add(openItem);
        menu.add(saveItem);
        return menu;
    }

    private class SaveAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            packetService.saveFile(Constants.SAVA_FILE_PCAP, jFrame);
        }
    }

    private class OpenAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            packetService.openFile(false, jFrame);
        }
    }

    private class AboutAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String text = "软件作者： 陈亮 \n" +
                    "软件功能：抓包，清洗，发包，检测，保存 \n" +
                    "本软件最终解释权归作者所有\n" +
                    "仅做学习使用，请勿将软件擅自商用";
            JOptionPane.showMessageDialog(aboutItem,text,"关于",1);
        }
    }

    private class TipItemAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String text = "提示";
            JOptionPane.showMessageDialog(aboutItem,text,"提示",1);
        }
    }

    private class SendPacketAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {


        }
    }

    private class snifferAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            SwingUtil.setBorderLayoutPane(HomeFrame.frame().getContentPane(), new CutFlowPanel(),BorderLayout.CENTER);
        }
    }

    private class cutAction implements ActionListener {
        //HomeJPanel homeJPanel = (HomeJPanel) HomeFrame.frame().getContentPane();
        @Override
        public void actionPerformed(ActionEvent e) {
            SwingUtil.setBorderLayoutPane(HomeFrame.frame().getContentPane(),new CutFlowPanel(),BorderLayout.CENTER);
        }
    }
}
